<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Lazy-Load</title>
    <style>
      .img {
        width: 200px;
        height: 200px;
        background-color: gray;
        margin: 10px 0;
      }
      .pic {
        width: 199px;
        height: 199px;
      }
    </style>
  </head>
  <body>
    <div class="container">
      <div class="img">
       
        <img class="pic" alt="加载中" data-src="../img/Cache-Control.png" />
      </div>
      <div class="img">
        <img class="pic" alt="加载中" data-src="../img/html.webp" />
      </div>
      <div class="img">
        <img class="pic" alt="加载中" data-src="../img/huiliu.webp" />
      </div>
      <div class="img">
        <img class="pic" alt="加载中" data-src="../img/qiao.webp" />
      </div>
      <div class="img">
        <img class="pic" alt="加载中" data-src="../img/webpack.png" />
      </div>
      <div class="img">
        <img class="pic" alt="加载中" data-src="../img/xieshang1.png" />
      </div>
      <div class="img">
        <img class="pic" alt="加载中" data-src="../img/youhua.webp" />
      </div>
      <div class="img">
        <img class="pic" alt="加载中" data-src="../img/-2.png" />
      </div>
      </div>
    </div>
  </body>
  <script>
    // 获取所有的图片标签
    const imgs = document.getElementsByTagName("img");
    // 获取可视区域的高度
    const viewHeight =
      window.innerHeight || document.documentElement.clientHeight;
    // num用于统计当前显示到了哪一张图片，避免每次都从第一张图片开始检查是否露出
    let num = 0;
    function lazyload() {
    console.log(11111111);
      for (let i = num; i < imgs.length; i++) {
        // 用可视区域高度减去元素顶部距离可视区域顶部的高度
        let distance = viewHeight - imgs[i].getBoundingClientRect().top;
        // 如果可视区域高度大于等于元素顶部距离可视区域顶部的高度，说明元素露出
        if (distance >= 0) {
          // 给元素写入真实的src，展示图片
          imgs[i].src = imgs[i].getAttribute("data-src");
          // 前i张图片已经加载完毕，下次从第i+1张开始检查是否露出
          num = i + 1;
        }
      }
    }
    // 监听Scroll事件
    window.addEventListener("scroll", lazyload, false);
  </script>
</html>
